package com.marvels.financial.loan.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.dto.AdjustmentFeeReviewDto;
import com.marvels.financial.loan.dto.McfeAdjustmentFeeDTO;
import com.marvels.financial.loan.dto.McfeTransactionCodeDTO;
import com.marvels.financial.loan.dto.PenaltyAdjustConditionDto;
import com.marvels.financial.loan.dto.PenaltyAdjustDto;
import com.marvels.financial.loan.service.PenaltyAdjustmentApi;

/**
 * 罚息调整
 * @author tangpw
 *
 */
@RestController
@RequestMapping("financial/loan/penaltyAdjustment/service")
public class PenaltyAdjustmentController {

	@Autowired
	private PenaltyAdjustmentApi penaltyAdjustmentApi;
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
	/**
	 * 罚息调整列表
	 * @param dto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("penaltyAdjustmentList")
	public MarvelsResultDTO penaltyAdjustmentList(@RequestBody PenaltyAdjustConditionDto dto){
		try {
			Page<PenaltyAdjustDto> list=penaltyAdjustmentApi.penaltyAdjustmentList(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询罚息调整列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询罚息调整列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询罚息调整列表异常", null);
		} 
	}

	
	/**
	 * 查询交易类型
	 * @return
	 */
	@ResponseBody
	@RequestMapping("tradeTypeList")
	public MarvelsResultDTO tradeTypeList(){
		try {
			List<McfeTransactionCodeDTO>  list=penaltyAdjustmentApi.queryCodeList();
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询查询交易类型列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询查询交易类型列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询查询交易类型列表异常", null);
		} 
	}
	
	
	
	/**
	 * 罚息调整历史列表
	 * @param dto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("penaltyAdjustmentHistory")
	public MarvelsResultDTO penaltyAdjustmentHistory(@RequestBody PenaltyAdjustConditionDto dto){
		try {
			Page<McfeAdjustmentFeeDTO>  list=penaltyAdjustmentApi.penaltyAdjustmentHistory(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询罚息调整历史列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询罚息调整历史列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询罚息调整历史列表异常", null);
		} 
	}
	
	
	
	
	
	/**
	 * 罚息调整申请
	 * @param dto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("penaltyAdjustmentApply")
	public MarvelsResultDTO penaltyAdjustmentApply(@RequestBody PenaltyAdjustConditionDto dto){
		try {
			penaltyAdjustmentApi.penaltyAdjustmentApply(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),null);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>罚息调整申请处理FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>罚息调整申请处理异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "罚息调整申请处理异常", null);
		} 
	}
	
	
	
	
	/**
	 * 罚息审核列表
	 * @param dto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("penaltyAdjustmentReview")
	public MarvelsResultDTO penaltyAdjustmentReview(@RequestBody PenaltyAdjustConditionDto dto){
		try {
			Page<AdjustmentFeeReviewDto>  list=penaltyAdjustmentApi.penaltyAdjustmentReview(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询罚息审核列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询罚息审核列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询罚息审核列表异常", null);
		} 
	}
	
	
	/**
	 * 罚息调整通过
	 * @param dto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("penaltyAdjustmentAdopt")
	public MarvelsResultDTO penaltyAdjustmentAdopt(@RequestBody PenaltyAdjustConditionDto dto){
		try {
			penaltyAdjustmentApi.penaltyAdjustmentAdopt(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>罚息调整通过审核FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>罚息调整通过审核异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "罚息调整通过审核异常", null);
		} 
	}
	
	
	
	
	/**
	 * 罚息调整拒绝
	 * @param dto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("penaltyAdjustmentRefuse")
	public MarvelsResultDTO penaltyAdjustmentRefuse(@RequestBody PenaltyAdjustConditionDto dto){
		try {
			penaltyAdjustmentApi.penaltyAdjustmentRefuse(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>罚息调整审核拒绝失败:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>罚息调整审核拒绝异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "罚息调整审核拒绝异常", null);
		} 
	}
	
	
	
	
	/**
	 * 罚息调整数据查询
	 * @param dto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("penaltyAdjustmentQuery")
	public MarvelsResultDTO penaltyAdjustmentQuery(@RequestBody PenaltyAdjustConditionDto dto){
		try {
			Page<AdjustmentFeeReviewDto> list=penaltyAdjustmentApi.penaltyAdjustmentQuery(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>罚息调整数据查询FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>罚息调整数据查询异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "罚息调整数据查询异常", null);
		} 
	}
	
}
